package com.itheima.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.constant.TokenConstant;
import com.itheima.domain.ShoppingCart;
import com.itheima.domain.User;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {


    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public R<List<ShoppingCart>> clientShoppingCartList(String token){

        String userStr = (String)redisTemplate.opsForValue().get(TokenConstant.USER_TOKEN_PREFIX + token);
        User user = JSON.parseObject(userStr, User.class);
        assert user != null;
        String userId = user.getId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);

        //通过条件只展示该登录用户的购物车
        List<ShoppingCart> list = list(lqw);
        return R.success(list);
    }


    @Override
    public R<ShoppingCart> clientAddShoppingCart(ShoppingCart shoppingCart){

        //获取到要减少的菜品或者套餐id
        String setmealId = shoppingCart.getSetmealId();
        String dishId = shoppingCart.getDishId();

        //设置查询条件该用户的套餐id
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(setmealId),ShoppingCart::getSetmealId,setmealId)
                .eq(StringUtils.isNotEmpty(dishId),ShoppingCart::getDishId,dishId)
                .eq(ShoppingCart::getUserId, TokenHolder.getCurrentId());


        ShoppingCart shoppingCart1= getOne(lqw);
        //判断购物车中是否有相同的菜品或套餐
        if (shoppingCart1==null){
            shoppingCart.setNumber(1);
        }else {
            //如果有获取到有的id
            String id1 = shoppingCart1.getId();
            removeById(id1);
            Integer number=shoppingCart1.getNumber()+1;

            //使用之前的id，不再去自动生成
            shoppingCart.setId(id1);
            shoppingCart.setNumber(number);
        }
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(TokenHolder.getCurrentId());

        boolean addResult = save(shoppingCart);
        if (addResult){
            return R.success(shoppingCart);
        }
        return R.error("添加异常");

    }


    @Override
    public R<ShoppingCart> clientSubShoppingCart(ShoppingCart shoppingCart){

        //获取到要减少的菜品或者套餐id
        String setmealId = shoppingCart.getSetmealId();
        String dishId = shoppingCart.getDishId();

        //设置查询条件该用户的套餐id
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(setmealId),ShoppingCart::getSetmealId,setmealId)
                .eq(StringUtils.isNotEmpty(dishId),ShoppingCart::getDishId,dishId)
                .eq(ShoppingCart::getUserId,TokenHolder.getCurrentId());
        ShoppingCart shoppingCart1= getOne(lqw);
        String id = shoppingCart1.getId();

        Integer number = shoppingCart1.getNumber();

        if (number>1){
            number=number-1;
            shoppingCart1.setNumber(number);
            boolean result = updateById(shoppingCart1);
            if (!result){
                return R.error("修改失败");
            }
        }else {
            removeById(id);
            return R.success(null);

        }


        return R.success(shoppingCart1);

    }

    @Override
    public R<String> clientShoppingCartClean(String token){
        String userStr = (String)redisTemplate.opsForValue().get(TokenConstant.USER_TOKEN_PREFIX + token);
        User user = JSON.parseObject(userStr, User.class);
        assert user != null;
        String userId = user.getId();

        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        boolean remove = remove(lqw);
        if (remove){
            return R.success("清空成功");
        }
        return R.error("清空失败");

    }
}
